/**
 * Copyright (c) Jan 15, 2013 3:29:16 PM WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.action.face;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.extblog.wkm.web.action.ActionController;
import org.extblog.wkm.web.service.face.FaceEditService;
import org.extblog.wkm.web.vo.BodyItemFaceView;
import org.extblog.wkm.web.vo.HomePageFaceView;
import org.extblog.wkm.web.vo.LeftItemFaceView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author WKM
 * FaceEditController.java history
 * 1.WKM - Jan 15, 2013 - Create File
 */
@Controller
@RequestMapping("/edit")
public class FaceEditController extends ActionController{
	
	/** 业务处理 **/
	private FaceEditService faceEditService = null;

	public FaceEditService getFaceEditService() {
		return faceEditService;
	}
	@Resource
	public void setFaceEditService(FaceEditService faceEditService) {
		this.faceEditService = faceEditService;
	}
	
	@RequestMapping(value="deleteRootById.wkm",method=RequestMethod.POST)
	@ResponseBody
	public Object deleteRootById(@ModelAttribute("view") LeftItemFaceView view){
		//headers={"content-type=application/json","content-type=application/xml"}
		log.info("获取到数据:" + view.getItemId());
		Map<String, String> map = new HashMap<String, String>();
		faceEditService.removeRootById(view.getItemId());
		map.put("success", "true");
		log.info("返回数据:" + map);
		return map;
	}
	
	@RequestMapping(value="saveRootById.wkm",method=RequestMethod.POST)
	@ResponseBody
	public Object saveRootById(@ModelAttribute("view") LeftItemFaceView view){
		//headers={"content-type=application/json","content-type=application/xmlcharset=utf8"}
		log.info("更新数据:" + view.getItemId());
		Map<String, String> map = new HashMap<String, String>();
		if(faceEditService.updateRootItem(view)){
			map.put("success", "true");
		} else {
			map.put("success", "false");
		}
		log.info("返回数据:" + map);
		return map;
	}
	
	
	@RequestMapping("loadRoot.wkm")
	@ResponseBody
	public Object loadRootItems(){
		log.info("获取所有根节点数据...");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("roots", faceEditService.getRootItemsToMap());
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
	@RequestMapping("loadChildren.wkm")
	@ResponseBody
	public Object loadChildrenItems(@ModelAttribute("view") LeftItemFaceView view){
		log.info("获取子节点数据...");
		Map<String,Object> map = new HashMap<String, Object>();
		if(view.getItemId() == null){
			log.info("获取所有子节点数据...");
			map.put("roots", faceEditService.getChildItemsToMap());
		} else {
			log.info("获取" + view.getItemId() + "子节点数据...");
			map.put("roots", faceEditService.getChildItemsToMap(view.getItemId()));
		}
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
	@RequestMapping("loadBodys.wkm")
	@ResponseBody
	public Object loadBodys(@ModelAttribute("view") BodyItemFaceView view){
		log.info("获取地址数据...");
		Map<String,Object> map = new HashMap<String, Object>();
		if(view.getItemParent() == null){
			log.info("根据ID=null获取所有地址数据.");
			map.put("roots", faceEditService.getBodyItems(view));
		} else {
			log.info("根据ID=" + view.getItemParent() + "获取地址数据.");
			map.put("roots", faceEditService.getBodyItems(view));
		}
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
	@RequestMapping("updateBody.wkm")
	@ResponseBody
	public Object updateBody(@ModelAttribute("view") BodyItemFaceView view){
		log.info("更新地址数据...");
		Map<String,Object> map = new HashMap<String, Object>();
		if(view.getItemId() == null){
			log.info("ID=null不能更新数据...");
			map.put("success", String.valueOf(Boolean.FALSE));
		} else {
			if(faceEditService.updateBodyItem(view)){
				log.info("更新ID=" + view.getItemId() + "数据成功.");
				map.put("success", String.valueOf(Boolean.TRUE));
			} else {
				log.info("更新ID=" + view.getItemId() + "数据失败.");
				map.put("success", String.valueOf(Boolean.FALSE));
			}
		}
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
	@RequestMapping("deleteBody.wkm")
	@ResponseBody
	public Object deleteBody(@ModelAttribute("view") BodyItemFaceView view){
		log.info("删除地址数据...");
		Map<String,Object> map = new HashMap<String, Object>();
		if(view.getItemId() == null){
			log.info("ID=null不能删除数据...");
			map.put("success", String.valueOf(Boolean.FALSE));
		} else {
			if(faceEditService.deleteBodyItem(view)){
				log.info("更新ID=" + view.getItemId() + "数据成功.");
				map.put("success", String.valueOf(Boolean.TRUE));
			} else {
				log.info("更新ID=" + view.getItemId() + "数据失败.");
				map.put("success", String.valueOf(Boolean.FALSE));
			}
		}
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
	@RequestMapping("loadHomePage.wkm")
	@ResponseBody
	public Object loadHomePage(){
		log.info("获取所有首页节点数据...");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("roots", faceEditService.loadHomePage());
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
	@RequestMapping("updateHomePage.wkm")
	@ResponseBody
	public Object saveHomePage(@ModelAttribute("view") HomePageFaceView view){
		log.info("获取所有首页节点数据...");
		Map<String,Object> map = new HashMap<String, Object>();
		if(view.getUrlId() == null){
			log.info("ID=null不能删除数据...");
			map.put("success", String.valueOf(Boolean.FALSE));
		} else {
			if(faceEditService.updateHomePage(view)){
				log.info("更新ID=" + view.getUrlId() + "数据成功.");
				map.put("success", String.valueOf(Boolean.TRUE));
			} else {
				log.info("更新ID=" + view.getUrlId() + "数据失败.");
				map.put("success", String.valueOf(Boolean.FALSE));
			}
		}
		
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
	@RequestMapping("deleteHomePage.wkm")
	@ResponseBody
	public Object deleteHomePage(@ModelAttribute("view") HomePageFaceView view){
		log.info("删除首页数据...");
		Map<String,Object> map = new HashMap<String, Object>();
		if(view.getUrlId() == null){
			log.info("ID=null不能删除数据...");
			map.put("success", String.valueOf(Boolean.FALSE));
		} else {
			if(faceEditService.deleteHomePage(view)){
				log.info("更新ID=" + view.getUrlId() + "数据成功.");
				map.put("success", String.valueOf(Boolean.TRUE));
			} else {
				log.info("更新ID=" + view.getUrlId() + "数据失败.");
				map.put("success", String.valueOf(Boolean.FALSE));
			}
		}
		try{
			log.info("向服务器返回数据:" + new ObjectMapper().writeValueAsString(map));
		} catch (Exception e){
			log.info("无法转换成JSON串");
		}
		return map;
	}
	
}
